package com.intexh.quickandroid.module.chat.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.intexh.quickandroid.module.chat.bean.ChatMessage;
import com.intexh.quickandroid.module.chat.ui.messageItemView.FaceMessageItem;
import com.intexh.quickandroid.module.chat.ui.messageItemView.ImageMessageItem;
import com.intexh.quickandroid.module.chat.ui.messageItemView.TextMessageItem;
import com.intexh.quickandroid.module.chat.ui.messageItemView.VoiceMessageItem;

import java.util.List;


/**
 * Created by engine on 13-12-17.
 */
public class AppChatAdapter extends BaseAdapter {
    private Context mContext;
    private List<ChatMessage> messagesList;
    private static final int VIEW_TYPE_TEXT = 0;
    private static final int VIEW_TYPE_IMAGE = 1;
    private static final int VIEW_TYPE_FACE = 2;
    private static final int VIEW_TYPE_VOICE = 3;
    private static final int VIEW_TYPE_BLEND=4;


    public AppChatAdapter(Context context, List<ChatMessage> list) {
        this.mContext = context;
        this.messagesList = list;
    }

    public void setData( List<ChatMessage> list){
        this.messagesList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return messagesList == null ? 0:messagesList.size();
    }

    @Override
    public ChatMessage getItem(int position) {
        return messagesList.get(position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        int messageType = messagesList.get(position).getType();

        switch(messageType){
            case ChatMessage.TYPE_TEXT:     //文字
                return VIEW_TYPE_TEXT;
            case ChatMessage.TYPE_IMAGE:    //图片
                return VIEW_TYPE_IMAGE;
            case ChatMessage.TYPE_FACE:     //表情
                return VIEW_TYPE_FACE;
            case ChatMessage.TYPE_VOICE:    //语音
                return VIEW_TYPE_VOICE;
            case ChatMessage.TYPE_BLEND://图文混合 -商品
                return VIEW_TYPE_BLEND;
            default:
                return VIEW_TYPE_TEXT;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 7;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ChatMessage chatMessage = getItem(position);
        boolean isNeedTime = needTimeView(position, chatMessage);
        switch(getItemViewType(position)){
            case VIEW_TYPE_TEXT:     //文字
                return new TextMessageItem(mContext, chatMessage, isNeedTime);
            case VIEW_TYPE_IMAGE:    //图片
                return new ImageMessageItem(mContext, chatMessage,messagesList, isNeedTime);
            case VIEW_TYPE_FACE:     //表情
                return new FaceMessageItem(mContext, chatMessage, isNeedTime);
            case VIEW_TYPE_VOICE:    //语音
                return new VoiceMessageItem(mContext, chatMessage, isNeedTime);
            case VIEW_TYPE_BLEND:    //图文
            default:
                throw new RuntimeException("ChatMessage type exception");
        }
    }

    private boolean needTimeView(int position, ChatMessage message) {
        if (position == 0) return true;
        ChatMessage preMessage = getItem(position - 1);
        return message.getCreate_time() - preMessage.getCreate_time() > 5 * 60 * 1000;
    }
}
